package com.lzh.redis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzh.redis.entity.User;
import com.lzh.redis.mapper.UserMapper;
import com.lzh.redis.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 1321303706@qq.com
 * @since 2021-11-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    //通过用户名查找对应用户
    public User queryUserByUsername(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        return userMapper.selectOne(wrapper);
    }
    //添加一个用户
    public int addUser(User user) {
        return userMapper.insert(user);
    }
    //更新用户
    public int updateUser(User user){
        return userMapper.updateById(user);
    }
    //通过id删除一个用户
    public int delUser(int id){
        return userMapper.deleteById(id);
    }
    public User queryUserById(int id){
        return userMapper.selectById(id);
    }
    //查询所有用户
    public List<User> queryUserList(){
        return userMapper.selectList(null);
    }

    /**
     * 分页查询药品数据
     *
     * @param pageNum  第几页
     * @param pageSize 每页多少条数据
     * @return
     */
    public List<User> selectUserPage(int pageNum, int pageSize) {
        IPage<User> page = new Page<>(pageNum,pageSize);
        IPage<User> userIPage = userMapper.selectPage(page,null);
        System.out.println("总页数"+userIPage.getPages());
        System.out.println("总记录数"+userIPage.getTotal());
        //getRecords()分页对象记录列表
        List<User> user = userIPage.getRecords();
        return user;
    }

}
